//
//  NSTimer+TYBlock.m
//  TYFoundation
//
//  Created by 郭艺伟 on 2019/2/28.
//

#import "NSTimer+TYBlock.h"


@interface TYTimerBlockHandler : NSObject
@property (nonatomic, copy) HandleTimerBlock block;
- (void)ty_handleBlockWithTimer:(NSTimer *)timer;
@end

@implementation TYTimerBlockHandler

@synthesize block;

- (void)ty_handleBlockWithTimer:(NSTimer *)timer
{
    HandleTimerBlock callbackBlock = self.block;
    
    NSAssert(callbackBlock != nil, @"Timer run block is nil");
    
    callbackBlock();
}

@end

@implementation NSTimer (TYBlock)
+ (NSTimer *)ty_scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(HandleTimerBlock)block repeats:(BOOL)inRepeats{
    
    TYTimerBlockHandler *blockHandler = [[TYTimerBlockHandler alloc] init];
    blockHandler.block = block;
    
    return [self scheduledTimerWithTimeInterval:seconds target:blockHandler selector:@selector(mup_handleBlockWithTimer:) userInfo:nil repeats:inRepeats];
}

+ (NSTimer *)mup_timerWithTimeInterval:(NSTimeInterval)seconds block:(HandleTimerBlock)block repeats:(BOOL)inRepeats{
    
    TYTimerBlockHandler *blockHandler = [[TYTimerBlockHandler alloc] init];
    blockHandler.block = block;
    
    return [self timerWithTimeInterval:seconds target:blockHandler selector:@selector(mup_handleBlockWithTimer:) userInfo:nil repeats:inRepeats];
    
}

@end
